package com.xzk.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzk.bean.BootStrapTableCourier;
import com.xzk.bean.Courier;
import com.xzk.bean.Message;
import com.xzk.bean.ResultData;
import com.xzk.exception.DuplicateIdCardException;
import com.xzk.exception.DuplicatePhoneException;
import com.xzk.service.CourierService;
import com.xzk.util.DateFormatUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CourierController
 * @Description 快递员
 * @Author 0715-YuHao
 * @Date 2020/9/14 16:08
 * @Version 1.0
 */
@Controller
@RequestMapping("/courier")
public class CourierController {

    @Resource(name = "courierService")
    private CourierService service;

    @RequestMapping("/console.do")
    public @ResponseBody Message console() {
        Map<String, Integer> data = service.console();
        Message msg = new Message();
        if (data.size() == 0) {
            msg.setStatus(-1);
        } else {
            msg.setStatus(0);
        }
        msg.setData(data);
        return msg;
    }

    @RequestMapping("/list.do")
    public @ResponseBody ResultData<BootStrapTableCourier> list(HttpServletRequest request, HttpServletResponse response) {
        //1. 获取分页查询数据的起始索引
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        //2. 获取当前页要查询的数据量
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        //3. 进行分页查询
        PageHelper.startPage(pageNumber, pageSize);
        List<Courier> list = service.findAll();
        PageInfo<Courier> info = new PageInfo<>(list);
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        int i = 1;
        for (Courier courier : info.getList()) {
            list2.add(new BootStrapTableCourier(
                    i++,
                    courier.getName(),
                    courier.getPhone(),
                    courier.getIdCard(),
                    "*******",
                    courier.getNumberDispatch(),
                    courier.getStatus() == 0 ? "已激活" : "已删除",
                    DateFormatUtil.formatDate(courier.getSignUpTime()),
                    courier.getLoginTime() == null ? "未登录" : DateFormatUtil.formatDate(courier.getLoginTime())
            ));
        }
        //4. 将集合封装为 bootstrap-table识别的格式
        ResultData<BootStrapTableCourier> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(info.getTotal());
        return data;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/insert.do")
    public @ResponseBody Message insert(HttpServletRequest request) {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        String password = request.getParameter("password");
        Message message = new Message();
        try {
            boolean insert = service.insert(new Courier(name, phone, idCard, password));
            if (insert) {
                message.setStatus(0);
                message.setContent("注册成功");
            }else {
                message.setStatus(-1);
                message.setContent("注册失败");
            }
        } catch (DuplicateIdCardException e) {
            message.setStatus(-1);
            message.setContent("身份证已被注册！");
        } catch (DuplicatePhoneException e) {
            message.setStatus(-1);
            message.setContent("手机号已被注册！");
        }
        return message;
    }

    @RequestMapping("/find.do")
    public @ResponseBody Message find(HttpServletRequest request) {
        String phone = request.getParameter("phone");
        Courier courier = service.findByPhone(phone);
        Message message = null;
        if (courier == null) {
            message = new Message(-1, "手机号码不存在");
        }else {
            message = new Message(0, "查询成功！");
            message.setData(courier);
        }
        return message;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/update.do")
    public @ResponseBody Message update(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        String password = request.getParameter("password");
        Message message = new Message();
        try {
            boolean update = service.update(id, new Courier(name, phone, idCard, password));
            if (update) {
                message.setStatus(0);
                message.setContent("修改成功");
            }else {
                message.setStatus(-1);
                message.setContent("修改失败");
            }
        } catch (DuplicatePhoneException e) {
            message.setStatus(-1);
            message.setContent("手机号已被注册！");
        } catch (DuplicateIdCardException e) {
            message.setStatus(-1);
            message.setContent("身份证已被注册！");
        }
        return message;
    }

    @RequestMapping("/delete.do")
    public @ResponseBody Message delete(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String phone = request.getParameter("phone");
        String idCard = request.getParameter("idCard");
        Message message = new Message();
        boolean delete = service.delete(id, phone, idCard);
        if (delete) {
            message.setStatus(0);
            message.setContent("删除成功");
        }else {
            message.setStatus(-1);
            message.setContent("删除失败");
        }
        return message;
    }
}
